package com.ruoyi.teaching.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.teaching.domain.vo.BatchHoliday;
import com.ruoyi.teaching.domain.Holiday;
import com.ruoyi.teaching.mapper.HolidayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.HolidayConfigMapper;
import com.ruoyi.teaching.domain.HolidayConfig;
import com.ruoyi.teaching.service.IHolidayConfigService;

/**
 * 节假日配置Service业务层处理
 *
 * @author sunshine
 * @date 2024-01-19
 */
@Service
public class HolidayConfigServiceImpl implements IHolidayConfigService
{
    @Autowired
    private HolidayConfigMapper holidayConfigMapper;

    @Autowired
    private HolidayMapper holidayMapper;
    /**
     * 查询节假日配置
     *
     * @param id 节假日配置主键
     * @return 节假日配置
     */
    @Override
    public HolidayConfig selectHolidayConfigById(Long id)
    {
        return holidayConfigMapper.selectHolidayConfigById(id);
    }

    /**
     * 查询节假日配置列表
     *
     * @param holidayConfig 节假日配置
     * @return 节假日配置
     */
    @Override
    public List<HolidayConfig> selectHolidayConfigList(HolidayConfig holidayConfig)
    {
        return holidayConfigMapper.selectHolidayConfigList(holidayConfig);
    }

    /**
     * 插入假日配置
     * 新增节假日配置
     *
     * @param holidayConfiglist 假日配置列表
     * @return 结果
     */
    @Override
    public int insertHolidayConfig(List<BatchHoliday> holidayConfiglist)
    {
        // 校验数据
        if (CollectionUtils.isEmpty(holidayConfiglist) || holidayConfiglist.size() < 6){
            throw new RuntimeException("节假日不能为空");
        }
//        节假日配置
        BatchHoliday batchHoliday = holidayConfiglist.get(6);
//取年
        String date = batchHoliday.getDate();
        String s = date.substring(0, 19).replaceAll("T", " ").substring(0,10);
        LocalDate localDate = LocalDate.parse(s,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        int year = localDate.getYear();
//        给节假日赋值
        HolidayConfig holidayConfig = new HolidayConfig();
        holidayConfig.setYear(String.valueOf(year));
//        将内容转为json字符串处理
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            holidayConfig.setContent(objectMapper.writeValueAsString(holidayConfiglist));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        holidayConfigMapper.insertHolidayConfig(holidayConfig);

//        然后遍历每一个节假日配置，然后添加到节假日中
        Long id = holidayConfig.getId();
        for (int i = 0; i < holidayConfiglist.size(); i++) {
            BatchHoliday holiday = holidayConfiglist.get(i);
//            然后一个一个设置
            date = holiday.getDate();
            s = date.substring(0, 19).replaceAll("T", " ").substring(0,10);
            localDate = LocalDate.parse(s,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            Integer count = holiday.getCount();
            String description = holiday.getDescription();
            for (int j = 0; j < count; j++) {

                holidayMapper.insertHoliday(new Holiday(localDate.toString(),localDate.getDayOfWeek().getValue(),description,id));
//                加一天，防止数据库添加时同一条数据
                localDate = localDate.plusDays(1);
            }
        }
        return 1;


    }

    /**
     * 修改节假日配置
     *
     * @param holidayConfig 节假日配置
     * @return 结果
     */
    @Override
    public int updateHolidayConfig(List<BatchHoliday> holidayConfig,Long id)
    {
//        将节假日的数据全删了，然后再删除节假日配置，然后再他调用方法添加

        holidayMapper.deleteHolidayByConfigId(id);
        holidayConfigMapper.deleteHolidayConfigById(id);
        return insertHolidayConfig(holidayConfig);

    }

    /**
     * 批量删除节假日配置
     *
     * @param ids 需要删除的节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteHolidayConfigByIds(Long[] ids)
    {
        return holidayConfigMapper.deleteHolidayConfigByIds(ids);
    }

    /**
     * 删除节假日配置信息
     *
     * @param id 节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteHolidayConfigById(Long id)
    {
        return holidayConfigMapper.deleteHolidayConfigById(id);
    }
}
